package com.freeBox;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class Login {

    @Autowired
    UserMapper uermapper; //自动注入

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping(value = {"/"  })
    public String mainPage() {
        return "main" ;
    }
    @GetMapping("/login")
    public String usrLogin(){
        return "login";
    }

    @PostMapping("/login")
    public String userCheck(@RequestParam("id") String id ,
                            @RequestParam("pwd") String pwd ,
                            HttpServletRequest request) {

        if(!id.isEmpty()&&!pwd.isEmpty()) {             //redis查询
             if(pwd.equals(getPwd(id))){
                 request.setAttribute("userlogin" , id);
                 return "platform";
             }

        }

            return "erro";
    }

    public String getPwd(String username) {
        try {
            ValueOperations<String , String> operations = redisTemplate.opsForValue() ;
            String pwd = operations.get(username);
            return pwd;
        } catch (Exception e) {
            return null;
        }
    }
}
